package org.jeecg.modules.common.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.Map;

public class TableToJson {

    //show create table c_orders  用这个语句获取
    public static final String createTableSql = "CREATE TABLE `ach_import_excel_analysis` (\n" +
            "  `id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '主键',\n" +
            "  `examination_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '试卷名称',\n" +
            "  `class_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '班级',\n" +
            "  `examination_no` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '考号',\n" +
            "  `student_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '姓名',\n" +
            "  `objective_exa` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '客观题',\n" +
            "  `subjective_exa` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '主观题',\n" +
            "  `total_exa` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '总分',\n" +
            "  `type_exa` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '类型',\n" +
            "  `topic_num` varchar(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '题号',\n" +
            "  `topic_value` varchar(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '得分',\n" +
            "  `topic_type` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '题类型',\n" +
            "  `examination_paper_id` varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '试卷ID',\n" +
            "  `class_id` varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '班级ID',\n" +
            "  `iz_open` int NOT NULL DEFAULT '1' COMMENT '是否启用(1是启用，0否停用)',\n" +
            "  `tenant_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '学校id',\n" +
            "  `sys_org_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '所属部门编码id',\n" +
            "  `del_flag` int NOT NULL DEFAULT '0' COMMENT '0表示未删除,1表示删除',\n" +
            "  `create_by` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',\n" +
            "  `create_time` datetime DEFAULT NULL COMMENT '创建时间',\n" +
            "  `update_by` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人',\n" +
            "  `update_time` datetime DEFAULT NULL COMMENT '修改时间',\n" +
            "  `grade_id` varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '年级ID',\n" +
            "  `examination_id` varchar(32) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '考试ID',\n" +
            "  PRIMARY KEY (`id`) USING BTREE\n" +
            ")";

    public static void main(String[] args) {
        StringBuffer sqls = new StringBuffer();
        sqls.append("select ");
        //业务名称 desc 业务编码名称生成后类名 name 主键 id  需要放到那个服务 shareName
        String newSql = createTableSql.substring(createTableSql.indexOf("(") + 1, createTableSql.lastIndexOf(")"));
        String tableName = createTableSql.substring(createTableSql.indexOf("TABLE") + 5, createTableSql.indexOf("(")).replace("`","").trim();
        String tableNameVo = StringUtil.lineToHump(tableName)+"Vo";
        newSql = newSql.replaceAll("\n", "");
        String[] rowSqls = newSql.split("',");
        JSONObject paramColumn = new JSONObject();
        JSONObject required = null;
        String key = "";
        for (String rowSql : rowSqls) {
            required = new JSONObject();
            key = rowSql.trim();
            key = key.substring(0, key.indexOf(" "));
            key = key.replaceAll("`", "");
            if ("UNIQUE".equals(key)) {
                continue;
            }
            if ("KEY".equals(key)) {
                continue;
            }
            if ("PRIMARY".equals(key)) {
                continue;
            }
            if ("create_time".equals(key)) {
                continue;
            }
            if ("update_time".equals(key)) {
                continue;
            }
            if ("create_by".equals(key)) {
                continue;
            }
            if ("update_by".equals(key)) {
                continue;
            }
            paramColumn.put(StringUtil.lineToHump(key), key);
            sqls.append("t."+key+" "+StringUtil.lineToHump(key)+",");
        }
        sqls = sqls.toString().endsWith(",")?new StringBuffer(sqls.substring(0,sqls.length()-1)):sqls;
        sqls.append(" from "+tableName+" t");
        System.out.println(sqls);
        System.out.println(dealUpdateInfoInstance(paramColumn,tableNameVo));
    }

    private static String dealUpdateInfoInstance(JSONObject paramsInfo,String tableNameVo) {

        Map<String, String> params = paramsInfo.toJavaObject(Map.class);
        String sql = "where 1=1\n";
        for (String key : params.keySet()) {
            if ("operate".equals(key)) {
                continue;
            }
            if ("sysOrgCode".equals(key)) {
            String keyTo = tableNameVo+"."+key;
            sql += "<if test=\"" + keyTo + " !=null and " + keyTo + " != ''\">\n";
            sql += "   and t." + params.get(key) + " in \n"+
            " <foreach collection=\""+keyTo+".split(',')\" item=\"item\" index=\"index\" separator=\",\" open=\"(\" close=\"(\" > #{item} </foreach>";
            sql += "</if> \n";
                continue;
            }
            String keyTo = tableNameVo+"."+key;
            sql += "<if test=\"" + keyTo + " !=null and " + keyTo + " != ''\">\n";
            sql += "   and t." + params.get(key) + "= #{" + keyTo + "}\n";
            sql += "</if> \n";
        }
//        sql += "<if test=\"pageNo != -1 and pageNo != null \">\n";
//        sql += "   limit #{pageNo}, #{pageSize}\n";
//        sql += "</if> \n";
        //endsWith 判断是否以指定字符结束
        sql = sql.endsWith(",") ? sql.substring(0, sql.length() - 1) : sql;
        return sql;
    }
}
